package server.main.utils.mainutils;

import java.io.*;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class FileUtils {
    /**
     * 实现整个文件夹中的所有文件和文件夹的转移
     * @param filePath1 源地址
     * @param filePath2 目的地址
     * @throws IOException
     */
    public static void CopyDir(String filePath1,String filePath2) throws IOException{
        File f1 = new File(filePath1);
        File f2 = new File(filePath2);
        File[] fs = f1.listFiles();
        if(!f2.exists()) {
            f2.mkdir();
        }
        for(File temp:fs) {
            if(temp.isDirectory()) {
                CopyDir(filePath1+File.separator+temp.getName(),filePath2+File.separator+temp.getName());
            }
            if(temp.isFile()) {
                CopyFile(filePath1+File.separator+temp.getName(),filePath2+File.separator+temp.getName());
            }
        }
    }

    public static void CopyFile(String filePath1,String filePath2) throws IOException{
        File oldFile = new File(filePath1);
        File newFile = new File(filePath2);
        FileInputStream in = new FileInputStream(oldFile);
        FileOutputStream out = new FileOutputStream(newFile);
        byte[] b = new byte[(int)oldFile.length()];
        in.read(b);
        in.close();
        out.write(b);
        out.close();
    }

    /**
     * 整个文件夹的剪切
     * @param filePath1 源地址
     * @param filePath2 目的地址
     * @throws IOException
     */
    public static void CutFile(String filePath1,String filePath2) throws IOException{
        File f1 = new File(filePath1);
        CopyDir(filePath1,filePath2);
        getDelete(f1);
    }

    public static void getDelete(File f) {
        File[] fs = f.listFiles();
        if(fs!=null) {
            for(File f2:fs) {
                if(f2.isFile()) {
                    f2.delete();
                }else if(f2.isDirectory()) {
                    getDelete(f2);
                    f2.delete();
                }
            }
        }
    }

    /**
     * 删除文件夹以及下面所有的文件
     * @param folderPath 文件夹名
     */
    public static void delFolder(String folderPath) {
        try {
            delAllFile(folderPath); //先删除这个文件夹下面的所有文件
            String filePath = folderPath;
            filePath = filePath.toString();
            java.io.File myFilePath = new java.io.File(filePath);
            myFilePath.delete(); //删除空文件夹
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static boolean delAllFile(String path) {
        boolean flag = false;
        File file = new File(path);
        if (!file.exists()) {
            return flag;
        }
        if (!file.isDirectory()) {
            return flag;
        }
        String[] tempList = file.list();
        File temp = null;
        for (int i = 0; i < tempList.length; i++) {
            if (path.endsWith(File.separator)) {
                temp = new File(path + tempList[i]);
            } else {
                temp = new File(path + File.separator + tempList[i]);
            }
            if (temp.isFile()) {
                temp.delete();
            }
            if (temp.isDirectory()) {
                delAllFile(path + "/" + tempList[i]);//先删除文件夹里面的文件
                delFolder(path + "/" + tempList[i]);//再删除空文件夹
                flag = true;
            }
        }
        return flag;
    }


    /**
     * 文件解压解压到指定目录
     * @param zipPath
     * @param descDir
     */
    public static void unZipFiles(String zipPath,String descDir)throws IOException{
        unZipFiles(new File(zipPath), descDir);
    }

    @SuppressWarnings("rawtypes")
    public static void unZipFiles(File zipFile, String descDir)throws IOException {
        System.out.println("******************解压开始********************");
        File pathFile = new File(descDir);
        if(!pathFile.exists()){
            pathFile.mkdirs();
        }
        ZipFile zip = new ZipFile(zipFile);
        for(Enumeration entries = zip.entries(); entries.hasMoreElements();){
            ZipEntry entry = (ZipEntry)entries.nextElement();
            String zipEntryName = entry.getName();
            InputStream in = zip.getInputStream(entry);
            String outPath = (descDir+ "\\" + zipEntryName).replaceAll("\\\\", "/");

            //判断路径是否存在,不存在则创建文件路径
            File file = new File(outPath.substring(0, outPath.lastIndexOf('/')));
            if(!file.exists()){
                file.mkdirs();
            }
            //判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压
            if(new File(outPath).isDirectory()){
                continue;
            }
            //输出文件路径信息
            System.out.println(outPath);

            OutputStream out = new FileOutputStream(outPath);
            byte[] buf1 = new byte[1024];
            int len;
            while((len=in.read(buf1))>0){
                out.write(buf1,0,len);
            }
            in.close();
            out.close();
        }
        System.out.println("******************解压完毕********************");
    }

    public static void main(String[] args) {
        try {



            CopyFile("D:\\important\\大四\\毕设\\备份\\bloodcharge-1.0-SNAPSHOT.jar","D:\\important\\大四\\毕设\\备份\\1\\new.jar");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
